$(function() {

	$(".get_verify_num").click(function() {

		var phone = $("#username").val();
		if(phone == "") {
			layer.msg("请输入电话号码");
			return false;
		} else if(!/^0?(13|14|15|17|18)[0-9]{9}$/i.test(phone)) {
			layer.msg("请输入正确的手机号码");
			return false;
		} else {
			$("#span").hide();
			time(this);
		}
		$.ajax({
			url: "https://newapp.icandrive.cn/api/code.php",
			data: {
				phone: phone
			},
			dataType: 'json',
			type: 'post',
			success: function(data) {
				if(data.status) {
					layer.msg("短信发送成功");
				} else {
					 layer.msg('系统繁忙',function(){
					     history.go(0)
					 },1000);
				}
			},
			error: function() {
				layer.msg('系统繁忙');
			}
		});
	});

	//验证码发送
	var wait = 30;

	function time(o) {
		if(wait == 0) {
			o.removeAttribute("disabled");
			o.value = "输入验证码";
			o.style.backgroundColor = "#fe9900";
			wait = 30;
		} else {
			o.setAttribute("disabled", true);
			o.value = wait + "秒后重新获取";
			o.style.backgroundColor = "#8f8b8b";
			wait--;
			setTimeout(function() {
				time(o)
			}, 1000)
		}
	};

	$(".login_btn").click(function() {
		var verify = $(".verify_num").val();
		var phone = $(".telePhone_num").val();
		if(verify == "") {
			layer.msg("验证码不能为空");

			return false;
		} else {
			$("#span").hide();
		}
		$.ajax({
			url: "https://newapp.icandrive.cn/api/login.php/login.php",
			type: 'post',
			dataType: 'json',
			data: {
				phone: phone,
				code: verify
			},
			success: function(data) {
				if(data.status == 500) {
					layer.msg("账号已锁,请联系客服")
				}
				if(data.status == 400) {
					layer.msg(data.msg);
				}
				if(data.status == 199) {
					window.location.href = "../startpage/stratpage.html";
				}
				if(data.status == 200) {
					console.log(data.token);
					// return false;
					localStorage.setItem('savalogintooken', data.token);
					localStorage.phone = data.token;

					if(data.role_id == 1) {
						localStorage.setItem('savestudentstatus', 1);
						localStorage.role_id_te = 1;
						window.location.href = "../index/index.html";
					}
					if(data.role_id == 2) {
						localStorage.setItem('savestudentstatus', 2);
						localStorage.role_id_st = 2;
						window.location.href = "../order/oder.html";
					}
				}
			},
			error: function() {
				layer.msg('系统繁忙');
			}
		});
	});

	/*监听手机返回按键并且带确认按钮 引用public_js公共文件 public*/
	confirm_return();
}) /*end $function*/

/*监听手机清楚运行程序  下次登录直接进入首页*/
run_token();
